<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>springboot+mybatis+mysql+layUI+thymeleaf测试</title>
    <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" />
    <script src="https://www.layuicdn.com/layui/layui.js"></script>
</head>
<body>
<!-- 内容主体 -->
<div class="main">
    <div class="books-pane">
        <button type="button" class="layui-btn layui-btn-fluid" id="btn_add"><i class="layui-icon">&#xe608;</i> 添加</button>
    </div>
    <div class="books-pane">
        <table class="layui-hide" id="student_table" lay-filter ="student_table"></table>
        <!--工具栏模板：-->
        <div type="text/html" id="toolbar" style="display: none;">
            <form class="layui-form layui-form-pane" lay-size="lg" style="text-align: left;height: 36px">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字搜索</label>
                        <div class="layui-input-inline">
                            <input placeholder="姓名" type="text" name="keyword" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn" lay-submit lay-filter="*">搜索</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/html" id="lineBtns">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['element', 'table', 'form', 'jquery','laydate'], function () {
        var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        table.render({
            elem: '#student_table'
            , url: '/student/list'
            , toolbar: '#toolbar'
            , title: '学员信息列表'
            , cols: [
                [
                    {field: 'id',  title: '序号', sort: true}
                    , {field: 'name',title: '姓名'}
                    , {field: 'sex', title: '性别', align: "center", templet: function(d){if(d.sex == 0){return '男'}else{return '女'}}}
                    , {field: 'age', title: '年龄', sort: true}
                    , {field: 'time', title: '出生日期', sort: true, templet : "<div>{{layui.util.toDateString(d.time, 'yyyy-MM-dd')}}</div>"}
                    , {field: 'remarks', title: '简介'}
                    , {fixed: 'right',  align:'right', toolbar: '#lineBtns'}
                ]
            ], page: true
        });
        form.on('submit(*)', function (data) {
            layer.msg("根据姓名查找");
            table.reload('student_table', {
                url: '/student/list'
                , where: {name: data.field.keyword} //设定异步数据接口的额外参数
            });
            return false;
        });
        //监听行工具事件
        table.on('tool(student_table)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data,//获得当前行数据
            layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
            } else if(layEvent === 'del'){
                layer.confirm('确定要删除当前信息吗?', function(index){
                    //向服务端发送删除指令
                    $.getJSON('/student/deleteStudentById',{id: data.id}, function(data){
                        if (data.status === 200) {
                            table.reload('student_table', {
                                url: '/student/list'
                            });
                            layer.msg(data.data);
                        } else if (data.status === 201) {
                            layer.msg(data.data);
                        }
                    });
                });
            } else if(layEvent === 'edit'){
                layer.open({
                    type: 2 ,
                    title:"修改学员信息",
                    area: ['500px', '600px'],
                    shadeClose:false,
                    content: "/student/edit?id="+data.id,
                });
            }
        });
        /**
         *   添加
         */
        $('#btn_add').on('click',function(index){
            layer.open({
                type: 2 ,
                title:"添加学员信息",
                area: ['500px', '600px'],
                shadeClose:false,
                content: "/student/add.html",
            });
        });
    });
</script>
</body>
</html>